
#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

bool TextNormalizer::IsCharNumber(const char& fpInChar)
{
	if(fpInChar >= '0' && fpInChar <= '9') {
		return true;
	}
	else {
		return false;
	}
}

bool TextNormalizer::IsStrAllNumber(const string& fpInSentence)
{
	for(const char tcChar : fpInSentence) {
		if(tcChar >= '0' && tcChar <= '9') {
		}
		else {
			return false;
		}
	}
	return true;
}

bool TextNormalizer::IsWstrAllNumber(const wstring& fpInWsentence)
{
	for(const wchar_t tcChar : fpInWsentence) {
		if(tcChar >= L'0' && tcChar <= L'9') {
		}
		else {
			return false;
		}
	}
	return true;
}


bool TextNormalizer::IsWcharZhLetter(const wchar_t& fpInWchar)
{
	if(fpInWchar >= 0x4E00 && fpInWchar <= 0x9FC8) {
	}
	else if(fpInWchar >= 0x3400 && fpInWchar <= 0x4DB5) {
	}
	else {
		return false;
	}
	return true;
}


bool TextNormalizer::IsWcharEnLetter(const wchar_t& fpInWchar)
{
	if(fpInWchar >= L'a' && fpInWchar <= L'z') {
	}
	else if(fpInWchar >= L'A' && fpInWchar <= L'Z') {
	}
	else {
		return false;
	}
	return true;
}

bool TextNormalizer::IsWcharNumber(const wchar_t& fpInWchar)
{
	if(fpInWchar >= L'0' && fpInWchar <= L'9') {
	}
	else {
		return false;
	}
	return true;
}

bool TextNormalizer::IsCharEnLetter(const char& fpInChar)
{
	if(fpInChar >= L'a' && fpInChar <= L'z') {
	}
	else if(fpInChar >= L'A' && fpInChar <= L'Z') {
	}
	else {
		return false;
	}
	return true;
}


int TextNormalizer::GetWstrFirstNumberPosition(const wstring& fpInWsentence)
{
	for(int i=0; i<fpInWsentence.length(); i++) {
		if(fpInWsentence[i] >= L'0' && fpInWsentence[i] <= L'9') {
			return i;
		}
	}
	return -1;
}

bool TextNormalizer::CheckIsDateNianyueri(const vector<string>& fpInVecDate)
{
	if(fpInVecDate.size() != 3) {
		return false;
	}

	bool tMaybeDateFlag = true;
	const string tStrYear = fpInVecDate[0];
	const string tStrMonth = fpInVecDate[1];
	const string tStrDay = fpInVecDate[2];

	if(tStrYear=="" || tStrMonth=="" || tStrDay=="") {
		return false;
	}
	if(tStrYear.length() != 4 || tStrMonth.length() > 2 || tStrDay.length() > 2) {
		return false;
	}

	const int tIntYear = stoi(tStrYear);
	const int tIntMonth = stoi(tStrMonth);
	const int tIntDay = stoi(tStrDay);
	if(tIntYear < 1000 || tIntYear > 9999 || tIntMonth < 1 || tIntMonth > 12 || tIntDay < 1 || tIntDay > 31) {
		return false;
	}
	return true;
}


vector<wstring> TextNormalizer::GetWstrPositionNextWords(const wstring& fpInWsentence, const size_t fcInPosition, const size_t fcInWordsNumber, const enum LanguageEnum fcInLanguage)
{
	size_t tPos = fcInPosition;
	wstring temp = L"";
	vector<wstring> tResult;
	for(size_t i=0; i<fcInWordsNumber; i++) {
		for(tPos; tPos<fpInWsentence.length(); tPos++) {
			if( fcInLanguage==LANG_ZH ? IsWcharZhLetter(fpInWsentence[tPos]) : IsWcharEnLetter(fpInWsentence[tPos]) ) {
				break;
			}
		}
		for(tPos; tPos<fpInWsentence.length(); tPos++) {
			if( fcInLanguage==LANG_ZH ? IsWcharZhLetter(fpInWsentence[tPos]) : IsWcharEnLetter(fpInWsentence[tPos]) ) {
				temp += fpInWsentence[tPos];
				if(fcInLanguage==LANG_ZH) {
					tPos ++;
					break;
				}
			}
			else {
				break;
			}
		}
		tResult.push_back(temp);
		temp = L"";
	}
	return tResult;
}


vector<wstring> TextNormalizer::GetWstrPositionLastWords(const wstring& fpInWsentence, const size_t fcInPosition, const size_t fcInWordsNumber, const enum LanguageEnum fcInLanguage)
{
	size_t tPos = fcInPosition;
	wstring temp = L"";
	vector<wstring> tResult;
	for(size_t i=0; i<fcInWordsNumber; i++) {
		for(tPos; (int)tPos>0; tPos--) {
			if( fcInLanguage==LANG_ZH ? IsWcharZhLetter(fpInWsentence[tPos]) : IsWcharEnLetter(fpInWsentence[tPos]) ) {
				break;
			}
		}
		for(tPos; (int)tPos>0; tPos--) {
			if( fcInLanguage==LANG_ZH ? IsWcharZhLetter(fpInWsentence[tPos]) : IsWcharEnLetter(fpInWsentence[tPos]) ) {
				temp = fpInWsentence[tPos] + temp;
				if(fcInLanguage==LANG_ZH) {
					tPos --;
					break;
				}
			}
			else {
				break;
			}
		}
		tResult.push_back(temp);
		temp = L"";
	}
	return tResult;
}


string TextNormalizer::NextWordsConnectAfterString(const vector<wstring> fcpInVecNextWords, const size_t fcInWordsNumber, const enum LanguageEnum fcInLanguage)
{
	wstring tWstrAfter = L"";
	const size_t tcWordsTotal = fcpInVecNextWords.size();
	if(tcWordsTotal >= fcInWordsNumber) {
		for(size_t i=0; i<fcInWordsNumber; i++) {
			if(i>0 && fcInLanguage==LANG_EN) {
				tWstrAfter += L" ";
			}
			tWstrAfter += fcpInVecNextWords[i];
		}
	}
	return wstring2utf8string(tWstrAfter);
}

string TextNormalizer::LastWordsConnectBeforeString(const vector<wstring> fcpInVecLastWords, const size_t fcInWordsNumber, const enum LanguageEnum fcInLanguage)
{
	wstring tWstrBefore = L"";
	const size_t tcWordsTotal = fcpInVecLastWords.size();
	if(tcWordsTotal >= fcInWordsNumber) {
		for(size_t i=0; i<fcInWordsNumber; i++) {
			if(i>0 && fcInLanguage==LANG_EN) {
				tWstrBefore += L" ";
			}
			tWstrBefore += fcpInVecLastWords[fcInWordsNumber-1-i];
		}
	}
	return wstring2utf8string(tWstrBefore);
}
